package com.eunion.boot.spring;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.eunion.core.LogUtil;

/**
 * 容器初始化后执行，监听。implements InitializingBean 亦可.
 * 
 * @since 2017年1月1日
 */
@Service
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	ProjectSettings projectSettings;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		//event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")
		if (event.getApplicationContext().getParent() == null) {
			String[] beanNames = event.getApplicationContext().getBeanDefinitionNames();
			LogUtil.get(this.getClass()).info("所有beanNames个数：{}", beanNames.length, Arrays.asList(beanNames));
			//			for (String bn : beanNames) {
			//				LogUtil.get(this.getClass()).info("Spring 容器中bean name:{}" , bn);
			//			}

			LogUtil.get(this.getClass()).info("Spring projectSettings:{}", projectSettings);
			SpringUtil.setProperties(projectSettings);
		}
	}
}
